package com.shuyou.fragments;

import java.util.ArrayList;
import java.util.Random;

import android.R.integer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.shuyou.R;
import com.shuyou.db.DBService;
import com.shuyou.db.Preferences;
import com.shuyou.net.BookDetail;
import com.shuyou.view.KeywordsFlow;

public class HotLabelFragment extends Fragment implements OnClickListener{

	private DBService dbService;
    private View rootView;
    private Button changeBtn;
    private KeywordsFlow keywordsFlow;
    private String[] keywords= { "QQ", "Sodino", "APK", "GFW", "铅笔",//  
            "短信", "桌面精灵", "MacBook Pro", "平板电脑", "雅诗兰黛",//  
            "卡西欧 TR-100", "笔记本", "SPY Mouse", "Thinkpad E40", "捕鱼达人",//  
            "内存清理", "地图", "导航", "闹钟", "主题",//  
            "通讯录", "播放器", "CSDN leak", "安全", "3D",//  
            "美女", "天气", "4743G", "戴尔", "联想",//  
            "欧朋", "浏览器", "愤怒的小鸟", "mmShow", "网易公开课",//  
            "iciba", "油水关系", "网游App", "互联网", "365日历",//  
            "脸部识别", "Chrome", "Safari", "中国版Siri", "A5处理器",//  
            "iPhone4S", "摩托 ME525", "魅族 M9", "尼康 S2500" };
    private ArrayList<BookDetail> list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //keywordsList = Arrays.asList(keywords);
        dbService = DBService.getInstance(getActivity());
        list = dbService.getHotWords();
        numList = new ArrayList<String>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.search_hot_label, null);
        changeBtn = (Button)rootView.findViewById(R.id.changeWordBtn);
        changeBtn.setOnClickListener(this);

        keywordsFlow = (KeywordsFlow)rootView.findViewById(R.id.keywordFlow_framelayout);
        keywordsFlow.SetActivity(this.getActivity());
        keywordsFlow.setDuration(800);
        keywordsFlow.setOnItemClickListener(this);
        feedKeywordsFlow(keywordsFlow, list);
        keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
        return rootView;
    }
    
    private void feedKeywordsFlow(KeywordsFlow keywordsFlow , ArrayList<BookDetail> list)
    {
    	if(list.isEmpty())
    	{
    		return;
    	}
    	Random random = new Random();  
    	int listSize = list.size() < KeywordsFlow.MAX ? list.size() : KeywordsFlow.MAX;
    	    	
    	for (int i = 0; i < listSize; i++) {   
            int ran = random.nextInt(list.size());
            while(isNumRepeat(ran))
            {
            	ran = random.nextInt(list.size());
            }
            String name = list.get(ran).getBook_name();  
            String isbn = list.get(ran).getIsbn();
            
            keywordsFlow.feedKeyword(name, isbn);
        }
        numList.clear();
    }
    private ArrayList<String> numList;//防止重复出现
    private boolean isNumRepeat(int num){
    	if(numList.contains(num+"")){
    		return true;
    	}
    	else
    	{
    		numList.add(num+"");
    		return false;
    	}
    }

	@Override
	public void onClick(View v) {
		
		switch(v.getId()) {
		case R.id.changeWordBtn:
			keywordsFlow.rubKeywords();
			feedKeywordsFlow(keywordsFlow, list);
			keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
			break;
		case R.id.keywordFlow_framelayout:
			
			break;
		default:
			break;	
		}
		
		
	}

    
}
